//
//  EmojiCollectionViewCell.m
//  YunShouHu
//
//  Created by WangQiang on 16/6/24.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "EmojiCollectionViewCell.h"
#import "EmojiCell.h"

#define ItemSizeW 24.0
@interface EmojiCollectionViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak ,nonatomic) UICollectionView *faceCollection;

@end
@implementation EmojiCollectionViewCell
static NSString *const ID = @"myCell";

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor clearColor];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(ItemSizeW, ItemSizeW);
        layout.minimumLineSpacing = 6.0;
         layout.sectionInset = UIEdgeInsetsMake(8.0, 5.0, 0, 5.0);
        layout.minimumInteritemSpacing = (APP_WIGHT-10.0 - ItemSizeW*9)/8.0;
        
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        UICollectionView * faceCollection = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        faceCollection.backgroundColor = [UIColor clearColor];
        faceCollection.delegate = self;
        faceCollection.dataSource = self;
        [faceCollection registerClass:[EmojiCell class] forCellWithReuseIdentifier:ID];
        faceCollection.showsVerticalScrollIndicator = NO;
        faceCollection.showsHorizontalScrollIndicator = NO;
        faceCollection.scrollEnabled = NO;
        
        self.faceCollection = faceCollection;
        [self addSubview:faceCollection];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.faceCollection.frame = self.bounds;
}
-(void)setDatas:(NSArray *)datas{
    _datas = datas;
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [self.faceCollection reloadData];
    [CATransaction commit];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datas.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.emoji = self.datas[indexPath.row];
  
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiEmotion *emoji = self.datas[indexPath.item];
    if([emoji.emojiEncoded isEqualToString:NSLocalizedString(@"删除", nil)]){
        if([self.delegate respondsToSelector:@selector(emojiViewDidDeleteAction)]){
            [self.delegate emojiViewDidDeleteAction];
        }
    }else{
        if([self.delegate respondsToSelector:@selector(emojiViewDidSelectedEmotion:)]){
            [self.delegate emojiViewDidSelectedEmotion:emoji];
        }
    }
    
}
@end
